"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lane.chang on '07/09/2022'
    comment: 自定义radio button
"""
from typing import Callable
from PyQt5.QtWidgets import QRadioButton, QLabel, QSpacerItem, QSizePolicy, QHBoxLayout, QButtonGroup, QWidget, QComboBox
from PyQt5.QtCore import Qt

from project.component import TITLE_FONT, CONTENT_FONT


class UserCombobox(QWidget):
    """ 自定义combobox
    """
    def __init__(self,
                 label_text: str = '',
                 object_name: str = '',
                 editable: bool = True,
                 items: list[str] = None,
                 gap=0,
                 parent=None):
        """
        :param label_text:
        :param object_name: 对象名称
        :param editable: 是否被允许编辑
        :param items:
        :param gap:
        :param parent:
        """
        super().__init__(parent)
        # 创建子控件
        self.label = QLabel(label_text, self)
        self.label.setFont(TITLE_FONT)

        self.combobox = QComboBox()
        self.combobox.setObjectName(object_name)
        self.combobox.setFont(CONTENT_FONT)
        self.combobox.setEditable(editable)
        self.combobox.setInsertPolicy(QComboBox.NoInsert)
        self.combobox.completer().setFilterMode(Qt.MatchContains)
        self.combobox.addItems(items)

        # 中间弹簧
        self.gap_spacer = QSpacerItem(gap, 0, QSizePolicy.Fixed, QSizePolicy.Minimum)

        # 布局
        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)  # 去掉外边距，可自由调整
        layout.addWidget(self.label)
        layout.addItem(self.gap_spacer)
        layout.addWidget(self.combobox)

    def text(self):
        """
        :return:
        """
        return self.combobox.currentText()

    def clear(self):
        """
        :return:
        """
        self.combobox.clear()

    def add_items(self, items: list[str]):
        """
        :param items:
        :return:
        """
        self.combobox.addItems(items)

    def set_text(self, value):
        """
        :return:
        """
        self.combobox.setCurrentText(value)

    def object_name(self):
        """
        :return:
        """
        return self.combobox.objectName()
